import request from '@/utils/request'

/**
 * 获取验证码
 */
export function getCaptcha() {
  return request.get<CaptchaResponse>('/auth/captcha')
}

/**
 * 用户登录
 */
export function login(data: LoginRequest) {
  return request.post<LoginResponse>('/auth/login', data)
}

/**
 * 用户登出
 */
export function logout() {
  return request.post('/auth/logout')
}

/**
 * 刷新 Token
 */
export function refreshToken() {
  return request.post<LoginResponse>('/auth/refresh')
}

/**
 * 获取当前用户信息
 */
export function getUserInfo() {
  return request.get<UserInfo>('/auth/userinfo')
}

/**
 * 获取用户菜单
 */
export function getUserMenus() {
  return request.get<MenuData[]>('/auth/menus')
}

/**
 * 获取用户权限
 */
export function getUserPermissions() {
  return request.get<string[]>('/auth/permissions')
}

/**
 * 获取用户列表
 */
export function getUserList(params: UserQueryParams) {
  return request.get<PageResponse<UserFormData>>('/users', params)
}

/**
 * 获取用户详情
 */
export function getUserById(id: number) {
  return request.get<UserFormData>(`/users/${id}`)
}

/**
 * 创建用户
 */
export function createUser(data: UserCreateRequest) {
  return request.post('/users', data, {
    successMessage: '创建用户成功'
  })
}

/**
 * 更新用户
 */
export function updateUser(id: number, data: UserUpdateRequest) {
  return request.put(`/users/${id}`, data, {
    successMessage: '更新用户成功'
  })
}

/**
 * 删除用户
 */
export function deleteUser(id: number) {
  return request.delete(`/users/${id}`, undefined, {
    successMessage: '删除用户成功'
  })
}

/**
 * 批量删除用户
 */
export function batchDeleteUsers(ids: number[]) {
  return request.delete(
    '/users/batch',
    { ids },
    {
      successMessage: '批量删除成功'
    }
  )
}

/**
 * 修改密码
 */
export function changePassword(data: ChangePasswordRequest) {
  return request.put('/users/password', data, {
    successMessage: '修改密码成功'
  })
}

/**
 * 重置密码
 */
export function resetPassword(id: number) {
  return request.put(`/users/${id}/password/reset`, undefined, {
    successMessage: '重置密码成功'
  })
}
